package com.ysyc.driver_ee.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.widget.CustomViewPager;
import com.ysyc.driver_ee.widget.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/29 17:56<br>
 * 版本： v1.0<br>
 */
public class CapacityFragment extends BaseFragment {
    private final String[] TAB_TITLES = new String[]{"全部", "无订单", "待装货", "待完成"};
    //Tab 数目
    private final int COUNT = TAB_TITLES.length;
    //Fragment 数组
    private final BaseFragment[] TAB_FRAGMENTS = new BaseFragment[COUNT];
    @BindView(R.id.title_capacity_view)
    TitleView titleCapacityView;
    @BindView(R.id.tl_capacity_view)
    TabLayout tlCapacityView;
    @BindView(R.id.vp_capacity_view)
    ViewPager vpCapacityView;

    public static final String TYPE_KEY = "type";
    public CapacityOrderFromFragment fragment;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_capacity;
    }
    @Override
    protected void onFragmentCreate(View view) {
        super.onFragmentCreate(view);
        initView();

    }

    private void initView() {
        titleCapacityView.setTitle("运力",getResources().getColor(R.color.white_ff));
        initTabLayout();
    }


    private void initTabLayout() {
        for (int i = 0; i < TAB_TITLES.length; i++) {
            Bundle bundle = new Bundle();
            bundle.putInt(TYPE_KEY,i);
            fragment = CapacityOrderFromFragment.newInstance(CapacityOrderFromFragment.class,bundle);
            TAB_FRAGMENTS[i] = fragment;
            tlCapacityView.addTab(tlCapacityView.newTab().setText(TAB_TITLES[i]));
        }
        vpCapacityView.setOffscreenPageLimit(3);
        CapacityFragment.MyViewPagerAdapter mAdapter = new CapacityFragment.MyViewPagerAdapter(getChildFragmentManager());
        vpCapacityView.setAdapter(mAdapter);
        vpCapacityView.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tlCapacityView));
        tlCapacityView.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(vpCapacityView));
    }

    /**
     * @description: ViewPager 适配器
     */
    private class MyViewPagerAdapter extends FragmentPagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TAB_FRAGMENTS[position];
        }

        @Override
        public int getCount() {
            return TAB_TITLES.length;
        }

    }


}
